package org.judo.admin;

import org.judo.admin.identity.AppUser;
import org.judo.components.uicontrol.ChoiceControl;
import org.judo.components.uicontrol.validation.MaxLength;
import org.judo.components.uicontrol.validation.MinLength;
import org.judo.components.uicontrol.validation.Required;
import org.judo.components.uicontrol.validation.ValidateOnAction;
import org.judo.controller.PageController;
import org.judo.database.Database;
import org.judo.database.JudoResultSet;
import org.judo.filter.ResultsFilter;

import java.util.Date;
import java.util.List;

import org.judo.propertyobject.Prop;
import org.judo.propertyobject.PropertyObject;
import org.judo.propertyobject.ValidationError;
import org.judo.controller.DefaultAction;

public class UserController extends PageController 
{
	@Prop String destinationURL;
	
	@Required @MinLength(6) @MaxLength(20) @ValidateOnAction("submit_user")
	@Prop String newUser;
	
	@Required @MinLength(6) @MaxLength(20) @ValidateOnAction("submit_user")
	@Prop String newPass;
	
	@Required @MinLength(6) @MaxLength(20) @ValidateOnAction("submit_user")
	@Prop String passAgain;
	
	@Prop private ChoiceControl status = new ChoiceControl();
	@Prop private ChoiceControl users = new ChoiceControl();
	
	private Database sysDB ;
	private int id ;
	private String username ;
	private String password ;
	private String firstname ;
	private String lastname ;
	private String email ;
	private String unit ;
	private String division ;
	private String title ;
	private String businessphone ;
	private String cellphone ;
	private Date adddate ;
	private String location ;
	private String street1 ;
	private String street2 ;
	private String city ;
	private String state_prov ;
	private String zip ;
	private String country ;
	private List results ;
	private String userid ;

	public void componentCreated()
	{
		status.addOption("NA","Select Option");
		status.addOption(1,"Active");
		status.addOption(2,"Inactive");
	}
	
	public void logout()
	{
		request.getSession().removeAttribute("__USER");
		showHomePage();
	}
	
	public void login()
	{
	}
	
	public void newUser()
	{
	}
	
	public void submitUser()
		throws Exception
	{
		if (!newPass.equals(passAgain))
			throw new ValidationError("Passwords","'Password' and 'New Password' must match");
		
		AppUser obj = new AppUser();
		obj.copyProps(this);
		
		obj.setUsername(newUser);
		obj.setPassword(newPass);
		obj.setStatus(1);	
		sysDB.insert("App_User",obj);
		
		showPage("/admin/user_controller/login.do");
	}
	
	public void logon()
		throws Exception
	{
		AppUser user = (AppUser)sysDB.find(AppUser.class,
			parse("select * from App_User where username = '${username}' and password = '${password}'"));

		if (user == null)
			throw new ValidationError("Login Attempt","Incorrect Login.  Please Try Again");
		
		String sql = "select url from Security_Rule,Site_Resource,Role,User_Role where Security_Rule.siteResourceId = Site_Resource.siteResourceId and Role.roleId = Security_Rule.roleId and User_Role.roleId = Role.roleId and userId = "+user.getUserid();
		JudoResultSet rs = sysDB.query(sql);
		
		for (int i=0;i<rs.size();i++)
		{
			PropertyObject url = (PropertyObject)rs.get(i);
			user.defineSecureURL((String)url.get("url"));
		}
		
		sql = "select roleName from User_Role,Role where User_Role.roleId = Role.roleId and userId = "+user.getUserid();
		rs = sysDB.query(sql);
		
		for (int i=0;i<rs.size();i++)
		{
			PropertyObject group = (PropertyObject)rs.get(i);
			user.addRole((String)group.get("rolename"));
		}
		
		request.getSession().setAttribute("__USER",user);
		
		String attemptPath = (String)request.getSession().getAttribute("__page_attempt");
	
		if (attemptPath != null)
			showPage(attemptPath);
		else
			showHomePage();
		
		request.getSession().removeAttribute("__page_attempt");
	}
	
	public void editUsers() throws Exception
	{
		PropertyObject obj = sysDB.find(parse("select * from App_User where USERID = ${id}"));
		copyProps(obj);
	}

	public void insertUsers() throws Exception,Exception
	{
		AppUser obj = new AppUser();	
		obj.copyProps(this);
		sysDB.insert("App_User",obj);
		showPage("/admin/user_controller/display_users.do");
	}

	@DefaultAction
	public void displayUsers() throws Exception
	{
		results = sysDB.query("select * from App_User");
		results = ResultsFilter.filterResults(new UserResultsFilter(),results);
	}
	
	public void detailUsers() throws Exception
	{
		PropertyObject obj = sysDB.find(parse("select * from App_User where USERID = ${id}"));
		copyProps(obj);
	}
	
	public void deleteUsers() throws Exception,Exception
	{
		showPage("/admin/user_controller/display_users.do");
		sysDB.delete(this,"delete from App_User where USERID = ${id}");
	}
	
	public void updateUsers() throws Exception,Exception
	{
		AppUser obj = (AppUser)sysDB.find(AppUser.class,id);
		obj.copyProps(this);
		sysDB.update("App_User",obj);
		showPage("/admin/user_controller/display_users.do");
	}

	public void setId(int id) 
	{
		this.id = id;
	}

	public int getId() 
	{
		return this.id;
	}

	public void setUsername(String username) 
	{
		this.username = username;
	}

	public String getUsername() 
	{
		return this.username;
	}

	public void setPassword(String password) 
	{
		this.password = password;
	}

	public String getPassword() 
	{
		return this.password;
	}

	public void setFirstname(String firstname) 
	{
		this.firstname = firstname;
	}

	public String getFirstname() 
	{
		return this.firstname;
	}

	public void setLastname(String lastname) 
	{
		this.lastname = lastname;
	}

	public String getLastname() 
	{
		return this.lastname;
	}

	public void setEmail(String email) 
	{
		this.email = email;
	}

	public String getEmail() 
	{
		return this.email;
	}

	public void setUnit(String unit) 
	{
		this.unit = unit;
	}

	public String getUnit() 
	{
		return this.unit;
	}

	public void setDivision(String division) 
	{
		this.division = division;
	}

	public String getDivision() 
	{
		return this.division;
	}

	public void setTitle(String title) 
	{
		this.title = title;
	}

	public String getTitle() 
	{
		return this.title;
	}

	public void setBusinessphone(String businessphone) 
	{
		this.businessphone = businessphone;
	}

	public String getBusinessphone() 
	{
		return this.businessphone;
	}

	public void setCellphone(String cellphone) 
	{
		this.cellphone = cellphone;
	}

	public String getCellphone() 
	{
		return this.cellphone;
	}

	public void setAdddate(Date adddate) 
	{
		this.adddate = adddate;
	}

	public Date getAdddate() 
	{
		return this.adddate;
	}

	public void setLocation(String location) 
	{
		this.location = location;
	}

	public String getLocation() 
	{
		return this.location;
	}

	public void setStreet1(String street1) 
	{
		this.street1 = street1;
	}

	public String getStreet1() 
	{
		return this.street1;
	}

	public void setStreet2(String street2) 
	{
		this.street2 = street2;
	}

	public String getStreet2() 
	{
		return this.street2;
	}

	public void setCity(String city) 
	{
		this.city = city;
	}

	public String getCity() 
	{
		return this.city;
	}

	public void setState_prov(String state_prov) 
	{
		this.state_prov = state_prov;
	}

	public String getState_prov() 
	{
		return this.state_prov;
	}

	public void setZip(String zip) 
	{
		this.zip = zip;
	}

	public String getZip() 
	{
		return this.zip;
	}

	public void setCountry(String country) 
	{
		this.country = country;
	}

	public String getCountry() 
	{
		return this.country;
	}

	public void createUsers() throws Exception
	{
	}

	public void setResults(List results) 
	{
		this.results = results;
	}

	public List getResults() 
	{
		return this.results;
	}

	public void setUserid(String userid) 
	{
		this.userid = userid;
	}

	public String getUserid() 
	{
		return this.userid;
	}
}
